//index.js
var hprose = require('../../utils/hprose.js');
var co = hprose.co;
var regeneratorRuntime = require("../../utils/regenerator-runtime.js");


//获取应用实例
const app = getApp()

Page({
  data: {
    motto: '欢迎参加考试，本次考试满分100分，答题时间为20分钟。在答题时请将头部对准前置摄像头，以防止未录入头像导致成绩无效。点击开始按钮后开始计时。',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    usersession:null
  },
  //事件处理函数
  stratExamTap: function() {
    var client = hprose.Client.create("https://www.josyx.club/Quiz_Exam/api", ["getExamStatusJsonAction"]);

    var that = this;

    client.getExamStatusJsonAction(
      JSON.stringify({
        'paperId': app.globalData.paperId,
        'session': app.globalData.session,
      })).then(function (result) {
        var row = JSON.parse(result);
        if (row.code == 201) {
          wx.navigateTo({
            url: '/pages/detail/detail'
          })
        }else if(row.code == 200){
          wx.navigateTo({
            url: 'main'
          })
        }else{
          console.log(row);
        }



      });  
  },
  onLoad: function () {
    wx.showShareMenu({
      withShareTicket: false
    });
    var client = hprose.Client.create("https://www.josyx.club/Quiz_People/api", ["loginJsonAction"]);

    var that = this;

    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      });
      console.log(1)
      console.log(app.globalData)
      client.loginJsonAction(
        JSON.stringify({ 
          'appid': 'wx7d7e2360a7632727',
          'sessionKey': app.globalData.code,
          'encryptedData': app.globalData.encryptedData,
          'iv': app.globalData.iv
        })).then(function (result) {
          var row = JSON.parse(result);
          app.globalData.session = row.session;
          that.setData({
            usersession: app.globalData.session 
          });
      });
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        });
        console.log(2)
         
        client.loginJsonAction(
          JSON.stringify({
            'appid': 'wx7d7e2360a7632727', 
            'sessionKey': app.globalData.code,
            'encryptedData': app.globalData.encryptedData,
            'iv': app.globalData.iv
          })).then(function (result) {
            console.log(result);
            console.log(444);
            var row = JSON.parse(result);
            
            app.globalData.session = row.session;

            that.setData({
              usersession: app.globalData.session
            });
          });
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
          console.log(3)
          console.log(app.globalData)
          client.loginJsonAction(
            JSON.stringify({
              'appid': 'wx7d7e2360a7632727',
              'sessionKey': app.globalData.code,
              'encryptedData': app.globalData.encryptedData,
              'iv': app.globalData.iv
            })).then(function (result) {
              console.log(result);
              var row = JSON.parse(result);
              app.globalData.session = row.session;
              that.setData({
                usersession: app.globalData.session
              });
            });
        }
      })
    }
  },
  getUserInfo: function(e) {
    console.log(e)
    var that = this;
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
    
    var client = hprose.Client.create("https://www.josyx.club/Quiz_People/api", ["loginJsonAction"]);
    client.loginJsonAction(
      JSON.stringify({
        'appid': 'wx7d7e2360a7632727',
        'sessionKey': app.globalData.code,
        'encryptedData': e.detail.encryptedData,
        'iv': e.detail.iv
      })).then(function (result) {
        console.log(result);
        var row = JSON.parse(result);
        app.globalData.session = row.session;
        that.setData({
          usersession: app.globalData.session
        });
      });



  }
})
